#include "login.h"

int do_login_first(user_session_t *user_session)
{
    char sql[4096]={0};
    sprintf(sql,"select username,salt,encrypted_password,is_deleted from users where username ='%s';",user_session->username);
    int qres=mysql_query(user_session->conn,sql);
    if(qres!=0)
    {
        fprintf(stderr,"%s:%s\n","mysql_query",mysql_error(user_session->conn));
        return -1;
    }
    printf("%s\n",sql);
    MYSQL_RES *table=mysql_store_result(user_session->conn);
    MYSQL_ROW row=mysql_fetch_row(table);
    char user_salt[4096]={0};

    if(row==NULL)
    {
        tlv_msg_t msg;
        bzero(&msg,sizeof(tlv_msg_t));
        char value[]="please register first.";
        msg.hdr.length=strlen(value);
        tlv_create(&msg,LOGIN_FALSE,msg.hdr.length,value);
        tlv_send(user_session->netfd,&msg);
        printf("please register first.\n");
        return -1;
    }
    else{

        if(atoi(row[3])==1)
        {
            tlv_msg_t msg;
            bzero(&msg,sizeof(tlv_msg_t));
            char value[]="user does not exists,please register again.";
            msg.hdr.length=strlen(value);
            tlv_create(&msg,LOGIN_FALSE,msg.hdr.length,value);
            tlv_send(user_session->netfd,&msg);
            printf("user does not exists,please register again.\n");
            return -1;
        }

        strcpy(user_salt,row[1]);
        printf("%s\n",user_salt);

        //发送盐值给客户端
        tlv_msg_t msg;
        bzero(&msg,sizeof(tlv_msg_t));
        msg.hdr.length=strlen(user_salt);
        tlv_create(&msg,LOGIN_IN,msg.hdr.length,user_salt);
        tlv_send(user_session->netfd,&msg);
        return 0;
    }
}

int do_login_second(user_session_t *user_session, char *encrypted_password)
{
    printf("recved password = %s\n",encrypted_password);
    char sql[4096]={0};
    sprintf(sql,"select username,id,encrypted_password from users where username ='%s';",user_session->username);
    int qres=mysql_query(user_session->conn,sql);
    if(qres!=0)
    {
        fprintf(stderr,"%s:%s\n","mysql_query",mysql_error(user_session->conn));
        return -1;
    }
    printf("%s\n",sql);
    MYSQL_RES *table=mysql_store_result(user_session->conn);
    MYSQL_ROW row=mysql_fetch_row(table);
    char user_encry_password[4096]={0};
    strcpy(user_encry_password,row[2]);
    printf("%s\n",user_encry_password);
    if(strcmp(user_encry_password,encrypted_password)==0)
    {
        tlv_msg_t msg;
        bzero(&msg,sizeof(tlv_msg_t));
        char value[]="login success.";
        msg.hdr.length=strlen(value);
        tlv_create(&msg,LOGIN_IN,msg.hdr.length,value);
        tlv_send(user_session->netfd,&msg);
        user_session->user_id=atoi(row[1]);
        strcpy(user_session->username,row[0]);
        printf("%s\n",user_session->username);
        printf("%d\n",user_session->user_id);
        printf("login success.\n");
        return 0;
    }
    else
    {
        tlv_msg_t msg;
        bzero(&msg,sizeof(tlv_msg_t));
        char value[]="password error.";
        msg.hdr.length=strlen(value);
        tlv_create(&msg,LOGIN_FALSE,msg.hdr.length,value);
        tlv_send(user_session->netfd,&msg);
        printf("password error!\n");
        return -1;
    }
    return 0;
}
